from cmath import phase
import numpy as np
import cv2
'''这个按定义写的算法跑不出来'''
class fourier_transformer:
    def __init__(self, img_path1):
        self.img = cv2.imread(img_path1)
        self.img_gray = []
        self.AM = []
        self.Phase = []
    

    def operation(self):
        [ H , W , C ] = self.img.shape
        print(H,W,C)
        self.img_gray = 0.2126 * self.img[:,:,0] + 0.7152 * self.img[:,:,1] + 0.0722 * self.img[:,:,2]
        temp = self.img_gray
        out = np.zeros((H,W),dtype=np.complex128)
        for u in range(H):
            for v in range(W):
                res=0
                for x in range(H):
                    for y in range(W):
                        res += temp[x,y]*np.exp(-1.j*2*np.pi*(u*x/H+v*y/W))
                out[u,v]=res
                print(v)
        self.AM=np.abs(out)
        self.Phase=phase(out)
        
    def show(self):
        out = np.hstack([self.img_gray, self.AM , self.Phase])
        cv2.imshow(out)
        cv2.waitKey(0)
        
if __name__ == "__main__":
    f1=fourier_transformer('YOIMIYA.jpg')
    f1.operation()
    f1.show()
        
        
        
      
      